1 /* 2 Copyright: Marcelo S. N. Mancini (Hipreme|MrcSnm), 2018 - 2021 3 License: [https://creativecommons.org/licenses/by/4.0/|CC BY-4.0 License]. 4 Authors: Marcelo S. N. Mancini 5 6 Copyright Marcelo S. N. Mancini 2018 - 2021. 7 Distributed under the CC BY-4.0 License. 8 (See accompanying file LICENSE.txt or copy at 9 https://creativecommons.org/licenses/by/4.0/ 10 */ 11 module hip.math.rect; 12 public import hip.math.vector; 13 14 struct Size 15 { 16 pure nothrow @nogc @safe: 17 uint width, height; 18 alias w = width, h = height; 19 20 Vector2 opCast() const { return Vector2(w, h);} 21 } 22 struct Rect 23 { 24 pure nothrow @nogc @safe: 25 float x = 0, y = 0, width = 0, height = 0; 26 alias w = width, h = height; 27 28 Vector2 position() const {return Vector2(x,y);} 29 Size size() const {return Size(cast(uint)w,cast(uint)h);} 30 31 32 void move(in float[2] vec){this.x+= vec[0];this.y+= vec[1];} 33 void move(in Vector2 vec){this.x+= vec[0];this.y+= vec[1];} 34 35 this(float x, float y, float w, float h){this.x=x;this.y=y;this.w=w;this.h=h;} 36 this(float[2] position, float w, float h){this(position[0], position[1], w, h);} 37 this(float[2] position, float[2] size){this(position[0], position[1], size[0], size[1]);} 38 this(float[2] position, Size size){this(position[0], position[1], size.w, size.h);} 39 this(float[4] rec){this(rec[0], rec[1], rec[2], rec[3]);} 40 41 this(int x, int y, int w, int h){this.x=x;this.y=y;this.w=w;this.h=h;} 42 this(int[2] position, int, int w, int h){this(position[0], position[1], w, h);} 43 this(int[2] position, int[2] size){this(position[0], position[1], size[0], size[1]);} 44 this(int[2] position, Size size){this(position[0], position[1], size.w, size.h);} 45 this(int[4] rec){this(rec[0], rec[1], rec[2], rec[3]);} 46 47 this(float[2] position, int w, int h){this(position[0], position[1], w, h);} 48 this(float[2] position, int[2] size){this(position[0], position[1], size[0], size[1]);} 49 50 this(int[2] position, float w, float h){this(position[0], position[1], w, h);} 51 this(int[2] position, float[2] size){this(position[0], position[1], size[0], size[1]);} 52 } 53 54 struct DynamicRect 55 { 56 pure nothrow @nogc @safe: 57 Rect rect; 58 Vector2 velocity; 59 Vector2 position() const {return Vector2(rect.x,rect.y);} 60 Size size() const {return Size(cast(uint)rect.w,cast(uint)rect.h);} 61 void move(in float[2] vec){rect.x+= vec[0];rect.y+= vec[1];} 62 void move(in Vector2 vec){rect.x+= vec[0];rect.y+= vec[1];} 63 64 }